package com.longder.kindergarten.service.impl;

import com.longder.kindergarten.entity.po.ClassGrade;
import com.longder.kindergarten.entity.po.Course;
import com.longder.kindergarten.repository.ClassGradeRepository;
import com.longder.kindergarten.repository.CourseRepository;
import com.longder.kindergarten.service.CourseManageService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CourseManageServiceImpl implements CourseManageService {

    @Resource
    private CourseRepository courseRepository;

    @Resource
    private ClassGradeRepository classGradeRepository;

    /**
     * 添加或修改课程（每日所学）
     */
    @Override
    public void addOrUpdateCourse(Course course) {
        // 如果是修改，不允许修改班级
        if(!ObjectUtils.isEmpty(course.getId())){
            Course dbCourse = courseRepository.getById(course.getId());
            dbCourse.setDate(course.getDate());
            dbCourse.setContent(course.getContent());
            courseRepository.save(dbCourse);
        }else{
            // 处理班级
            ClassGrade classGrade = classGradeRepository.findById(course.getClassId()).orElseThrow();
            course.setClassGrade(classGrade);
            courseRepository.save(course);
        }
    }

    /**
     * 每日所学列表（课程列表）
     */
    @Override
    public List<Course> listCourse() {
        return null;
    }

    /**
     * 根据id查询某个课程
     */
    @Override
    public Course getCourse(Long courseId) {
        return null;
    }

    @Override
    public List<Course> listByClassId(Long classId) {
        return courseRepository.listByClassId(classId);
    }
}
